<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css" type="text/css"/>
<title>DEMO</title>
<script type="text/javascript" src="js/apiv1.3.min.js"></script>
<!--script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script-->
<link rel="stylesheet" type="text/css" href="bmap.css" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>
</head>
<body>
	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
			<li><a href="#">首页</a>
			</li>
			<li><a href="#">线路管理</a>
			</li>
			<li><a href="#">线路删除</a>
			</li>
		</ul>
	</div>

	<div
		style="width:100%;height:70%;border:1px solid gray;position:absolute;"
		id="container"></div>
		
		<div id="addRoute" style="position:absolute;top:80%;left:0%;">
		
		线路路线:
		<select id="rouLine" class="newdeleteRoutLine">
		
		</select>
		<input type="button"  class="btn"   value="删除路线" id="btn_end" onclick="setdeleteRoutLine()" />

	</div>
</body>
</html>
<script type="text/javascript">
	$(function(){
		show_routeLine();
	});
	
	function show_routeLine(pageIndex)
	{
		
		$.post("route!findList.action",function(data)
		{	
			var operation="<option selected='selected'>请选择</option>";
			var bean = eval("("+data+")");
			for(var i = 0 ; i < bean.length;i++){
				operation += "<option value="+bean[i].id+">"+bean[i].name+"</option>";
			}
			$("#rouLine").html(operation);
		
		})
		
	}
	
	function setdeleteRoutLine() {
		
		var id=$("#rouLine").val();
		
		okCancelAlert("删除路线","确定删除此路线?",function(){
		$.post("route!deleteRoutLine.action",{cid:id},function(data){
			var bean = eval("("+data+")");
			
			if(bean.code==1){
				location.href="deleterouteLine.html";
			}else if(bean.code==0){
				defaultAlert("删除路线","删除失败");
			}else{
				defaultAlert("删除路线","未知错误");
			}
		});
	},function(){});
	}
	
	
	var mapOptions = {
		mapType : BMAP_NORMAL_MAP,
		maxZoom : 10, //地图最大层级
		minZoom : 5
	}
	var map = new BMap.Map("container", mapOptions); //设置卫星图为底图BMAP_PERSPECTIVE_MAP
	var initPoint = new BMap.Point(116.404, 39.915); // 创建点坐标

	map.centerAndZoom(initPoint, 8); // 初始化地图,设置中心点坐标和地图级别。
	map.enableScrollWheelZoom(); // 启用滚轮放大缩小。
	map.enableKeyboard(); // 启用键盘操作。  
	map.enableContinuousZoom(); //启用连续缩放效果

	map.addControl(new BMap.NavigationControl()); //地图平移缩放控件
	map.addControl(new BMap.ScaleControl()); //显示比例尺在右下角
	
	map.addEventListener("zoomend", function() {
		var overlays = map.getOverlays();
		for ( var i = 0; i < overlays.length - 1; i++) {
			if (overlays[i]._config.icon) {
				if (overlays[i]._config.icon.imageUrl.indexOf("images/circle")!=-1) {
					if (map.getViewport().zoom <= 7) {
						overlays[i].hide();
					} else {
						overlays[i].show();
					}
				}
			}
		}
	});


	function addPole(lng, lat, opts,color) {
		var point = new BMap.Point(lng, lat);
		var myIcon = new BMap.Icon("images/circle.png", new BMap.Size(16, 16));

		var marker = new BMap.Marker(point, {
			icon : myIcon,
			enableDragging : false,
			draggingCursor : "move",
			title : opts
		});
		
		
	
		marker.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		
		
		map.addOverlay(marker);
		return marker;
	}
	
	var routeDiv;
	function addRoute(points, opts, color) {

		var polyline = new BMap.Polyline(points, {
			strokeColor : color,
			strokeWeight : 3,
			strokeOpacity : 0.8
		});
		polyline.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
		});
		polyline.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		map.addOverlay(polyline);
	}
		$.post("pole!findList.action", function(data) {
		var polies = eval("("+data+")");
		var arr=["","_red","_yellow"];
		for ( var i = 0; i < polies.length; i++) {
			addPole(polies[i].lng, polies[i].lat, polies[i].id,arr[i%3]);
		}
	});
//findAllPolies
	$.post("route!findAllPolies.action",function(data){
			var json = eval("("+data+")");
			for(var i=0;i<json.length;i++){
				var polies = json[i];
				var points = new Array();
				var name = "";
				var points1 = new Array();
				for ( var x = 0; x < polies.length; x++) {
					points[x] = new BMap.Point(polies[x].lng,polies[x].lat);
					name = polies[x].routeName;
				}
				addRoute(points, name, "green");
  				var meter = 0;
				for ( var m = 1; m < points.length; m++) {
					meter += map.getDistance(points[m - 1],points[m]);
				}
			}
	});


	
	function addRouteLine(){
	var ids=$("#startNO").html()+","+$("#add_polies").html().split("<br>").join(",")+","+$("#endNO").html();

	
		$.post("route!addCircuitry.action",{
		"poleIds":ids,
		"circuitry.length":$("#route_length").html(),
		"circuitry.name": $("#lxmc").val(),
		"circuitry.id": $("#lxid").val(),
		"circuitry.voltage" :$("#lxdy").val(),
		"circuitry.poleByEndpoleNo.id" :$("#endNO").html(),
		"circuitry.poleByStartpoleNo.id" :$("#startNO").html()
		},function(data){
		var json = eval("("+data+")");
			switch(json.code){
				case 1: alert("添加成功!");
				setTimeout(function () { location.href="routeLine.html"; }, 2000);
				;break;
				default : alert("添加失败");
				
			}
		});
	}
	
</script>
